<?php

namespace App\Store\Controllers\Sysset;

use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Sysset\SigninUsemoney;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Validate\Sysset\SigninUsemoney as ValidateSigninUsemoney;

/**
 * 签到积分
 */
class SigninUsemoneyController extends Controller
{
    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['use_money_min', 'use_money_max', 'rate']);
        $Validate = new ValidateSigninUsemoney('add');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        if ($req_data['use_money_max'] <= $req_data['use_money_min']) {
            return response()->json(['code' => '001', 'msg' => '使用金额范围有误']);
        }

        // 组装数据
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        $data = $req_data;
        $data['cp_id']                    = $cp_id;
        $data['create_account_id']        = $accountinfo['id'];
        $data['create_account_real_name'] = $accountinfo['real_name'];
        $data['create_at']                = time();

        SigninUsemoney::create($data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $request['cp_id'] = $accountinfo['cp_id'];
        $list = SigninUsemoney::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 详情
     */
    public function getone(Request $request)
    {
        $one = SigninUsemoney::where(['id'=>$request['id'],'is_del'=>1])->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '000', 'msg' => '获取失败']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['id', 'use_money_min', 'use_money_max', 'rate']);
        $Validate = new ValidateSigninUsemoney('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $data = $req_data;
        unset($data['id']);
        $data['update_account_id']        = $accountinfo['id'];
        $data['update_account_real_name'] = $accountinfo['real_name'];
        $data['update_at']                = time();
        SigninUsemoney::where('id', $req_data['id'])->update($data);

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidateSigninUsemoney('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $req_data['is_del']                   = 2;
        $req_data['update_account_id']        = $accountinfo['id'];
        $req_data['update_account_real_name'] = $accountinfo['real_name'];
        $req_data['update_at']                = time();

        SigninUsemoney::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 通过金额获取详情
     */
    public function getonebymoney(Request $request)
    {
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'] ?? 0;
        $one = SigninUsemoney::query()->where(['cp_id' => $cp_id, 'is_del' => 1])
            ->where([
                ['use_money_min', '<=', $request['money']],
                ['use_money_max', '>=', $request['money']]
            ])
            ->orderBy('id','desc')
            ->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => ['use_money_min' => 0, 'use_money_max' => 0, 'rate' => 0]]);
    }

    /**
     * 详情
     */
    public function getlastone(Request $request)
    {
        $one = SigninUsemoney::where('is_del', 1)->orderBy('id','desc')->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '000', 'msg' => '获取失败']);
    }
}
